SprintBoot MyBatis 查数据数据返回 Object、Map、List、Map<List> 小结 |
您所在的位置:网站首页 › return list › SprintBoot MyBatis 查数据数据返回 Object、Map、List、Map<List> 小结 |
前言: SpringBoot + MyBatis 框架进行接口返回时,常常需要封装一下对象类似JSON数据结构的操作,案例通过常见web接口数据结构几种进行演示。 代码层级 --PackageName 包名 ----test 目录 ------controller 控制层:定义url,控制入参 ------entity 对象层:这里指User对象的属性定义 ------maaper 数据库语句定义层:sql数据的增删改查查询书写 ------service 服务层:调用sql语句并进行一些逻辑判断处理 TestMyBatisController.java package com.qabujiaban.benjamin.test.controller; /** * @Author: Benjamin * @Date: 2021/8/12 11:37 */ import com.qabujiaban.benjamin.test.entity.User; import com.qabujiaban.benjamin.test.service.TestMyBatisService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; /** 流程 1、controller 控制层,url定义的逻辑定义 2、maaper 原始sql语句 3、service 参数处理及分页原理 4、entity 对象属性 */ @RestController public class TestMyBatisController { @Autowired private TestMyBatisService testMyBatisService; @RequestMapping("/getUserId") public User getUserId(Integer id){ return testMyBatisService.getUserInfo(id); } @RequestMapping("/getAllUser") public List getAllUser(){ return testMyBatisService.getAllUser(); } @RequestMapping("/getAllUserPro") public Map getAllUserPro(){ Map result = new HashMap(); result.put("errorCode",0); result.put("errorMeg","成功"); result.put("AllUserInfo",testMyBatisService.getAllUser()); return result; } @RequestMapping("/getUserIdMap") public Map getUserIdMap(Integer id){ return testMyBatisService.getUserIdMap(id); } }User.java package com.qabujiaban.benjamin.test.entity; /** * @Author: Benjamin * @Date: 2021/8/12 11:41 */ public class User { public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public String getCreate_time() { return create_time; } public void setCreate_time(String create_time) { this.create_time = create_time; } private int id; private String username; private String password; private String ip; private String create_time; }为了方便对比查看贴图表明User表中的数据 TestMyBatisMapper.java package com.qabujiaban.benjamin.test.maaper; /** * @Author: Benjamin * @Date: 2021/8/12 11:37 */ import com.qabujiaban.benjamin.test.entity.User; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; import java.util.List; import java.util.Map; @Mapper public interface TestMyBatisMapper { @Select("select * from user where id = #{id}") User findUserId(@Param("id") Integer id); @Select("select * from user") List findAllUser(); @Select("select * from user where id = #{id}") Map findUserIdMap(@Param("id") Integer id); }判空和未判空任选其一即可 TestMybatisService.java 未做判空处理 package com.qabujiaban.benjamin.test.service; import com.qabujiaban.benjamin.test.entity.User; import com.qabujiaban.benjamin.test.maaper.TestMyBatisMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Map; /** * @Author: Benjamin * @Date: 2021/8/12 11:37 */ @Service public class TestMyBatisService { @Autowired private TestMyBatisMapper testMyBatisMapper; public User getUserInfo(Integer id){ return testMyBatisMapper.findUserId(id); } public List getAllUser(){ return testMyBatisMapper.findAllUser(); } public Map getUserIdMap(Integer id){ return testMyBatisMapper.findUserIdMap(id); } }TestMyBatisService.java 已做判空处理 package com.qabujiaban.benjamin.test.service; import com.qabujiaban.benjamin.test.entity.User; import com.qabujiaban.benjamin.test.maaper.TestMyBatisMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * @Author: Benjamin * @Date: 2021/8/12 11:37 */ @Service public class TestMyBatisService { @Autowired private TestMyBatisMapper testMyBatisMapper; public User getUserInfo(Integer id){ User user = testMyBatisMapper.findUserId(id); if(user == null){ return new User(); } return user; } public Map getUserIdMap(Integer id){ Map user = testMyBatisMapper.findUserIdMap(id); if(user == null){ return new HashMap(); } return user; } public List getAllUser(){ List user = testMyBatisMapper.findAllUser(); if(user == null){ return new ArrayList(); } return testMyBatisMapper.findAllUser(); } }代码层级截图 接口效果展示 return object; /getUserId 为空 /getUserId 不为空 return map; /getUserIdMap 为空 /getUserIdMap 不为空 return list; getAllUser 为空 /getAllUser 不为空 return Map; /getAllUserPro 为空 /getAllUserPro 不为空 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |